package com.witsight.platform.util.io;

import java.io.InputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**   
 * 说明：XML解析工具类
 * @Title: XMLUtil.java 
 * @Package com.witsight.platform.util.io 
 * @See: {@link #XMLUtil()}
 * Copyright: Copyright (c) 2017
 * Company:sany huax witsight team by product
 * @author: penght  
 * @date: 2017年9月20日 上午11:20:56 
 * @version: V1.0
 *
 */
public abstract class XMLUtil {
	/**
	 * 说明：定义后能再同一类中使用static常量log
	 */
	private static final Logger LOGGER = LoggerFactory.getLogger(XMLUtil.class);
	
	/**
	 * 
	 * 说明：xml解析成class
	 * @Title: xml2Bean
	 * @author penght 
	 * @param is 文件流
	 * @param beanClass 转换为bean
	 * @return 
	 * @throws BaseException 
	 * @date: Aug 17, 2017 10:25:50 AM
	 */
	@SuppressWarnings("unchecked")
	public static <T> T xml2Bean(InputStream is, Class<T> beanClass) throws JAXBException {
		try {
			JAXBContext jaxbContext = JAXBContext.newInstance(beanClass);

			Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
			T object = (T) jaxbUnmarshaller.unmarshal(is);
			return object;
		} catch (JAXBException e) {
			if (LOGGER.isErrorEnabled()) {
				LOGGER.error("xml change bean error.",e);
			}
			throw e;
		}
	}
}
